<include file="public@header"/>
<link rel="stylesheet" href="__STATIC__/js/jquery-ui/jquery-ui-timepicker-addon.min.css">


<body>
<div class="wrap js-check-wrap">
    <ul class="nav nav-tabs">
        <li class="active"><a href="{:url('CourseSchedule/index')}">排课列表</a></li>
    </ul>
    <form class="well form-inline margin-top-20 row" method="post" action="{:url('CourseSchedule/index')}">
        <label class="form-group col-lg-5 col-md-6" style="margin-bottom:15px">
            学校-班级:
            <select class="form-control" name="school_id" id="school_id" style="width:179px;">
                <option value='0'>全部</option>
                <notempty name="school">
                    <volist name="school" id="vo">
                        <if condition="isset($param.school_id) && $param.school_id == $vo.id">
                            <option value="{$vo.id}" selected>{$vo.name}</option>
                        <else/>
                            <option value="{$vo.id}">{$vo.name}</option>
                        </if>
                    </volist>
                </notempty>
            </select> -
            <select class="form-control" name="class_id" id="class_id" style="width:179px;">
                <option value='0'>全部</option>
                <notempty name="classes">
                    <volist name="classes" id="vo">
                        <if condition="isset($param.class_id) && $param.class_id == $vo.id">
                            <option value="{$vo.id}" selected>{$vo.name}</option>
                            <else/>
                            <option value="{$vo.id}">{$vo.name}</option>
                        </if>
                    </volist>
                </notempty>
            </select>
        </label>

        <label class="form-group col-lg-4 col-md-3" style="margin-bottom:15px">
            课程:
            <select class="form-control" name="course_id" id="course_id" >
                <option value='0'>全部</option>
                <notempty name="course">
                    <volist name="course" id="vo">
                        <if condition="isset($param.course_id) && $param.course_id == $vo.id">
                            <option value="{$vo.id}" selected>{$vo.course_name}</option>
                            <else/>
                            <option value="{$vo.id}">{$vo.course_name}</option>
                        </if>
                    </volist>
                </notempty>
            </select>
        </label>

        <label class="form-group col-lg-5 col-md-6">
            上课时间:&nbsp;&nbsp;
            <input type="text" class="form-control" name="start"
                   value="{$param.start|default=''}"
                   autocomplete="off"> -
            <input type="text" class="form-control" name="end"
                   value="{$param.end|default=''}"
                   autocomplete="off">
        </label>

        <label class="form-group col-lg-4 col-md-3">
             <input type="submit" class="btn btn-primary" value="搜索"/>
            <a class="btn btn-danger" href="{:url('CourseSchedule/index')}">清空</a>
        </label>
    </form>

    <form class="col-xs-4 col-md-1 form-group" style="float:right; margin-top:-74px; margin-right:300px;"
          method="post" action="">
        <label class="form-group col-lg-4 col-md-3">
            <a class="btn btn-success" href="javascript:;" onclick="addSchedule(1)" title="额外加课">加课</a>
            {$arr_error|default=''}
        </label>
    </form>

    <form class="col-xs-4 col-md-1 form-group" style="float:right; margin-top:-74px; margin-right:230px;"
          method="post" action="">
        <label class="form-group col-lg-4 col-md-3">
            <a class="btn btn-info" href="javascript:;" onclick="addSchedule(2)" title="调整排课，总课时不变">调课</a>
            {$arr_error|default=''}
        </label>
    </form>

    <form class="js-ajax-form" action="" method="post">
        <table class="table table-hover table-bordered table-list">
            <thead>
            <tr>
                <th>编号</th>
                <th>学校</th>
                <th>班级</th>
                <th>教室</th>
                <th>课程</th>
                <th>授课老师</th>
                <th>课时进度</th>
                <th>上课日期</th>
                <th>上课时段</th>
                <th>星期</th>
                <!--<th>上课状态</th>-->
                <th>操作</th>
            </tr>
            </thead>
            <notempty name="schedule">
                <foreach name="schedule" item="vo">
                    <tr>
                        <td>{$vo.id}</td>
                        <td>{$vo.school_name}</td>
                        <td>{$vo.class_name}</td>
                        <td>{$vo.classroom_name}</td>
                        <td>{$vo.course_name}</td>
                        <td>{$vo.user_name}</td>
                        <td>{$vo.schedule}/{$vo.course_hour}</td>
                        <td>{$vo.date|date="Y-m-d",###}</td>
                        <td>{$vo.start_time}~{$vo.end_time}</td>
                        <td>{$vo.week}</td>
                        <!--<if condition="$vo.status eq 0">-->
                            <!--<td>未上</td>-->
                        <!--<else/>-->
                            <!--<td>已上</td>-->
                        <!--</if>-->
                        <if condition="$vo.schedule eq 0">
                            <td>
                                <a href="{:url('CourseSchedule/deleteSchedule',array('id'=>$vo['id']))}" class="js-ajax-delete">{:lang('DELETE')}</a>
                            </td>
                        <else/>
                            <td></td>
                        </if>
                    </tr>
                </foreach>
            </notempty>
        </table>
        <div class="pagination">{$page|default=''}</div>
    </form>
</div>

<div id="add_course" style="display: none;">
    <form id="form_sub" action="" class="row well" style="margin:0;">
        <input type="hidden" name="schedule_school" id="schedule_school" value="0">
        <input type="hidden" name="schedule_class" id="schedule_class" value="0">
        <input type="hidden" name="is_adjust" id="is_adjust" value="0">

        <label class="form-group form-inline col-xs-12" style="margin-bottom:15px">
            本校教室:&nbsp;&nbsp;
            <select class="form-control" name="classroom_id" id="classroom_id" style="width:179px;">
                <option value='0'>--- 请选择 ---</option>
            </select>
        </label>

        <label for="new_teacher" class="form-group col-xs-12 form-inline">授课老师:&nbsp;&nbsp;
            <input type="text" class="form-control" id="new_teacher" name="new_teacher" value="" style="width:179px;">
            <input type="hidden" class="form-control" id="teacher_id" name="teacher_id" value="">
        </label>

        <div id="time_group1" class="row" style="padding-left: 15px;" >
            <label for="input_date" class="form-group col-xs-3" style="padding-right: 0px;">
                上课日期:<span class="form-required">*</span>
                <input type="text" class="form-control" id="input_date" name="schedule_date[]" placeholder="月/日/年">
            </label>

            <label for="input_time_start" class="form-group col-xs-3" style="padding-right: 0px;">
                上课开始时间:<span class="form-required">*</span>
                <input type="text" class="form-control" id="input_time_start" name="schedule_time_start[]" placeholder="时:分">
            </label>

            <label for="input_time_end" class="form-group col-xs-3" style="padding-right: 0px;">
                上课结束时间:<span class="form-required">*</span>
                <input type="text" class="form-control" id="input_time_end" name="schedule_time_end[]" placeholder="时:分">
            </label>

            <div class="col-xs-3 " style="padding-top:20px;">
                <a class="btn btn-success" id="add_time">添加</a>
            </div>
        </div>
    </form>
</div>

<script src="__STATIC__/js/admin.js"></script>
<script type="text/javascript" src="__STATIC__/js/jquery-ui/jquery-ui-timepicker-addon.min.js"></script>
<script type="text/javascript" src="__STATIC__/js/jquery.autocomplete.js"></script>
<script>
    $(document).ready(function () {
        $("#input_date").datepicker();
        $("#input_time_start").timepicker();
        $("#input_time_end").timepicker();

        //自动补全老师
        var url = "{:url('information/Information/ajax_user')}";
        $('#new_teacher').devbridgeAutocomplete({
            serviceUrl: url,
            autoSelectFirst: true,
            minChars: 1,
            showNoSuggestionNotice: true,
            onSelect: function (suggestion) {
                //设置老师
                $('#teacher_id').val(suggestion.data);
            }
        });

        //筛选学校
        $('#school_id').on('change',function () {
            var sid = $(this).val();
            if(sid == 0){
                $('#class_id').empty();
                var html = '<option value=\"0\">全部</option>';
                $('#class_id').html(html);
                return false;
            }
            $.ajax({
                url:"{:url('CourseSchedule/ajax_class')}",
                type:'post',
                dataType:'json',
                data:{"school_id":sid},
                success:function(jsonData){
                    $('#class_id').empty();
                    var html = '<option value=\"0\">全部</option>';

                    if(jsonData.code == 1){
                        for(var i in jsonData.data){
                            html += '<option value=\"'+jsonData.data[i].id+'\">'+jsonData.data[i].name+'</option>';
                        }
                    }
                    $('#class_id').html(html);
                },
                error:function(XMLHttpRequest, textStatus, errorThrown){
                    console.log('xhr='+XMLHttpRequest.readyState+' status='+textStatus+' error='+errorThrown);
                }
            });
        });

        $('input[name="start"]').datepicker();
        $('input[name="end"]').datepicker();
    });

    //临时加课弹窗
    function addSchedule(is_adjust) {
        if(isNaN(is_adjust)){
            layer.msg('请求有误');
            return false;
        }else {
            //加课和调课的区分标记
            $('#is_adjust').val(is_adjust);
        }

        //学校
        var schoolId = $('#school_id').val();
        if(schoolId==undefined || schoolId=="" || schoolId==null || schoolId==0){
            layer.msg('请选择学校');
            return false;
        }
        $('#schedule_school').val(schoolId);
        //通过学校获取教室
        $.ajax({
            url:"{:url('CourseSchedule/ajax_classroom')}",
            type:'post',
            dataType:'json',
            data:{"school_id":schoolId},
            success:function(jsonData){
                $('#classroom_id').empty();
                var html = '<option value=\"0\">--- 请选择 ---</option>';
                if(jsonData.code == 1){
                    for(var i in jsonData.data){
                        html += '<option value=\"'+jsonData.data[i].id+'\">'+jsonData.data[i].name+'</option>';
                    }
                }
                $('#classroom_id').html(html);
            },
            error:function(XMLHttpRequest, textStatus, errorThrown){
                console.log('xhr='+XMLHttpRequest.readyState+' status='+textStatus+' error='+errorThrown);
            }
        });

        var classId = $('#class_id').val();
        if(classId==undefined || classId=="" || classId==null || classId==0){
            layer.msg('请选择班级');
            return false;
        }
        //保存班级
        $('#schedule_class').val(classId);

        layer.open({
            type: 1,
            btn: ['提交'],
            title: '添加课时',
            area:'600px',
            content: $('#add_course'),
            yes: function (index, layero) {
                loading = layer.load(0, { shade: false });
                add_course_schedule();
            }
        });
    }

    //加课或调课
    function add_course_schedule() {
        $.ajax({
            url:"{:url('CourseSchedule/addCourseSchedule')}",
            type:'post',
            dataType:'json',
            data: $('#form_sub').serialize(),
            success:function(jsonData){
                layer.msg(jsonData.Message, { icon: jsonData.Status, time: 1500 }, function () {
                    layer.closeAll();
                    window.location.reload();
                });
            },
            error:function(XMLHttpRequest, textStatus, errorThrown){
                console.log('xhr='+XMLHttpRequest.readyState+' status='+textStatus+' error='+errorThrown);
            }
        });

    }

    //添加课程时间段;
    $('body').on('click','#add_time',function () {
        var $time_date=$('#input_date').val();
        var $time_start=$('#input_time_start').val();
        var $time_end=$('#input_time_end').val();
        //判断时间非空和大小
        if (!$time_date || !$time_start || !$time_end){
            layer.msg('请选择日期和时间后再添加');
            return false;
        }else if($time_start.split(':')[0]>$time_end.split(':')[0]){
            layer.msg('开始时间不能迟于结束时间');
            return false;
        }else if($time_start.split(':')[0]==$time_end.split(':')[0]){
            if($time_start.split(':')[1]> $time_end.split(':')[1]){
                layer.msg('开始时间不能迟于结束时间');
                return false;
            }
        }

        var $new_group=
            $('<div class="time_group row" style="padding-left: 15px;">' +
                    '<label for="input_date" class="form-group col-xs-3" style="padding-right: 0px;">'+
                        '上课日期:'+
                        '<input type="text" class="form-control"  name="schedule_date[]" value="' +$time_date+ '" readonly>'+
                    '</label>'+

                    '<label for="input_time_start" class="form-group col-xs-3" style="padding-right: 0px;">'+
                        '开始时间:'+
                        '<input type="text" class="form-control"  name="schedule_time_start[]" value="' +$time_start+'" readonly>'+
                    '</label>'+

                    '<label for="input_time_end" class="form-group col-xs-3" style="padding-right: 0px;">'+
                        '结束时间:'+
                        '<input type="text" class="form-control"  name="schedule_time_end[]" value="'+$time_end+'" readonly>'+
                    '</label>'+

                    '<div class="col-xs-3" style="padding-top:20px;">'+
                        '<a class="btn btn-success remove_time" >'+
                            '取消'+
                        '</a>'+
                    '</div>'+
            '</div>');
        $new_group.insertAfter($('#time_group1'));
        $('#input_date').val('');
        $('#input_time_start').val('');
        $('#input_time_end').val('');
    });

    $('body').on('click','.remove_time',function () {
        $(this).parents('.time_group').remove();
    });



</script>
</body>